package com.springboot.springautowired.esStudy;/**
 * @author <a href="mailto:donghui.ma@ikang.com">donghui.ma</a>
 * @description
 * @date 2021-09-26 17:12
 **/

import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;
import java.util.stream.IntStream;

/**
 *
 * @author <a href="mailto:donghui.ma@ikang.com">donghui.ma</a>
 *
 * @description  需要明确的概念：index 索引 对应数据库中的数据库；
 *                              doc  文档  对应数据库中的表
 *
 *
 * @date 2021-09-26 17:12
 **/
@Component
public class ESClass {

    @Autowired
    private RestHighLevelClient client;

    public void littleMethod() throws IOException {
        //----创建索引
        CreateIndexRequest mdh = new CreateIndexRequest("mdh");
        //客户端执行请求 获得创建索引之后的响应
        CreateIndexResponse createIndexResponse = client.indices().create(mdh, RequestOptions.DEFAULT);
        System.out.println("createIndexResponse = " + createIndexResponse);

        //----获取索引，判断是否存在
        GetIndexRequest mdh1 = new GetIndexRequest("mdh");
        boolean exists = client.indices().exists(mdh1, RequestOptions.DEFAULT);
        System.out.println("exists = " + exists);

        //----删除索引
        DeleteIndexRequest mdh2 = new DeleteIndexRequest("mdh");
        AcknowledgedResponse delete = client.indices().delete(mdh2, RequestOptions.DEFAULT);
        System.out.println("delete.isAcknowledged() = " + delete.isAcknowledged());

        //----添加文档
        //创建对象
        User user = new User("Redmi G", 1);
        //创建请求
        IndexRequest xiaomi_index = new IndexRequest("xiaomi_index");
        //设置规则  put /xiaomi_index/_doc/1
        xiaomi_index.id("1");
        xiaomi_index.timeout(TimeValue.timeValueSeconds(1));
        xiaomi_index.timeout("1s");
        //将数据放入请求 json
        xiaomi_index.source(JSON.toJSONString(user),XContentType.JSON);
        //客户端发送请求，获取响应结果
        IndexResponse index = client.index(xiaomi_index, RequestOptions.DEFAULT);
        System.out.println(index.toString());
        System.out.println(index.status());

        //----获取文档 判断是否存在 get /index/doc/1
        GetRequest redmi_g = new GetRequest("Redmi G", "1");
        //不获取返回的_source的上下文
        redmi_g.fetchSourceContext(new FetchSourceContext(false));
        redmi_g.storedFields("_none_");
        //判断是否存在
        boolean exists1 = client.exists(redmi_g, RequestOptions.DEFAULT);
        System.out.println("exists1 = " + exists1);

        //--获取文档信息
        GetResponse documentFields = client.get(redmi_g, RequestOptions.DEFAULT);
        //打印文档内容
        System.out.println(documentFields.getSourceAsString());
        //返回的内容和命令是否一致
        System.out.println(documentFields);

        //--更新文档
        UpdateRequest redmi_g1 = new UpdateRequest("Redmi G", "1");
        redmi_g1.timeout("1s");
        User macBook_pro = new User("MacBook Pro", 12);
        redmi_g1.doc(JSON.toJSONString(macBook_pro),XContentType.JSON);
        UpdateResponse update = client.update(redmi_g1, RequestOptions.DEFAULT);
        System.out.println(update.status());

        //--删除文档记录
        DeleteRequest deleteRequest = new DeleteRequest("Redmi G", "1");
        deleteRequest.timeout("1s");
        DeleteResponse delete1 = client.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(delete1.status());

        //--批量插入数据
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10s");

        List<User> list = new ArrayList<>();
        IntStream.range(0,10).forEach(i->list.add(new User(String.valueOf(i),i)));

    }
}
